<?php


namespace App\Models;


use App\Helpers\Traits\Models\TimeScope;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

/**
 * App\Models\BaseModel
 *
 * @property-read mixed $created_at
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel createdAtAfter($date)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel createdAtBefore($date)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel createdAtBetween($start, $end)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel createdAtOfDay($data)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel lastLoginTimeAfter($date)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel lastLoginTimeBefore($date)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel lastLoginTimeBetween($start, $end)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel query()
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel updatedAtAfter($date)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel updatedAtBefore($date)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel updatedAtBetween($start, $end)
 * @mixin \Eloquent
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel sizeAfter($data)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel sizeBefore($data)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel sizeBetween($start, $end)
 */
class BaseModel extends Model
{
    use TimeScope;
    use Scope;

    //    use ExtenHasRelationships;//测试
    protected $guarded = ['created_at', 'updated_at', 'deleted_at'];
    protected $defaultCasts = ['enable_status' => 'bool', 'param' => 'json', 'request' => 'json'];
    protected $hidden = ['password', '_lft', '_rgt'];

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        parent::mergeCasts($this->defaultCasts);
    }

    public static function new()
    {
        return new static();
    }

    public static function getTableName()
    {
        return with(new static)->getTable();
    }

    public static function getMorphClassName()
    {
        return with(new static)->getMorphClass();
    }

    protected static function boot()
    {
        parent::boot();
        static::saving(function (self $obj) {
            if (!empty($obj->version)) {
                $obj->version = \DB::raw('version + 1');
            }
        });
    }

    public function getTable()
    {
        return $this->table ?? Str::snake(class_basename($this));
    }

    public function getCreatedAtAttribute($value)
    {
        return $value;
    }

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }
}
